{extend name="base"}
{block name="style"}
<!-- 这里允许在引入一些样式头部 -->
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet" href="{:_static()}/layui/css/layui.css" media="all" />
<link rel="stylesheet" href="//at.alicdn.com/t/font_tnyc012u2rlwstt9.css" media="all" />
<link rel="stylesheet" href="{:_static()}/css/source.css" media="all" />
<link rel="stylesheet" href="{:_static()}/css/common.css" media="all" />

{/block}

{block name="main"}
<style type="text/css">
	.ws-main{padding: 15px;}
	a:hover{color: inherit !important;}
	label{box-sizing: content-box;}
</style>

	<div class="childrenBody">
    <blockquote class="layui-elem-quote news_search">
        <div class="layui-inline layui-form">
            <a class="layui-btn layui-btn-normal sourceDelete_btn">删除</a>
            <a class="layui-btn layui-btn-normal sourceAdd_btn">新增素材小组</a>
        </div>
    </blockquote>
    <div class="layui-form">
        <table class="layui-table">
            <thead>
                <tr>
                    <th><input type="checkbox" name="" lay-skin="primary" lay-filter="allChoose"></th>
                    <th>编号</th>
                    <th>
                        <!-- 属性 -->
                        <select id="sucai_type" name="type" lay-filter="type">
                            <option value="">属性</option>
                            <option value="0">纯文字</option>
                            <option value="1">图文</option>
                            <option value="2">视频</option>
                        </select>     
                              
                    </th>
                    <th>
                        <!-- 素材小组 -->
                        <select id="sucai_cate" name="cate" lay-filter="cate">
                            <option value="">素材小组</option>
                            {volist name="catelist" id="data"}
                            <option value="{$data.id}">{$data.cate_name}</option>
                            {/volist}
                            <!-- <option value="021">图文</option>
                            <option value="0571">视频</option> -->
                        </select>     
                    </th>
                    <th>标题</th>
                    <th>文字内容</th>
                    <th>内容</th>
                    <th>评论内容</th>
                    <th>操作</th>
                </tr> 
            </thead>
            <tbody id="mainhtml">
                {volist name="list" id="data"  empty="暂时没有数据" }
                <tr>
                    <td><input type="checkbox" name="delcheck" value="{$data.id}" lay-skin="primary"></td>
                    <td>{$data.id}</td>
                    <td>{if $data.type eq 0}文字{elseif $data.type eq 1/}图片{elseif $data.type eq 2/}视频{/if}</td>
                    <td>{$data.cate_name}</td>
                    <td>{$data.title}</td>
                    <td>{$data.content}</td>
                    <td>{if $data.mtype eq 0}无{elseif $data.mtype eq 1/}<img src="/uploads{$data.link}" width="90">{elseif $data.mtype eq 2/}<video width="90" height="60" controls="controls" src="/uploads{$data.link}"></video>{/if}</td>
                    <td>{$data.comment}</td>
                    <td>
                        <button class="layui-btn layui-btn-normal layui-btn-mini" onclick="edit({$data.id});"><i class="layui-icon"></i>编辑</button>
                        <button class="layui-btn layui-btn-danger layui-btn-mini" onclick="del({$data.id});"><i class="layui-icon"></i>删除</button>
                    </td>
                </tr>
                {/volist}
            </tbody>
        </table>
        <!-- 分页 -->
        <div id="pagination1"></div>
    </div>
    <div id="page"></div>
	</div>

    <div id="edit" style="position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); display: none;">
        <div class="layui-form" style="margin: 0 auto; margin-top: 100px; width: 500px; background-color: #fff;">
            <div class="row row1" style="width: 500px;">
                <div class="filter">
                    <span class="" style="display: block; float: right; margin: 15px 15px 0 0;" onclick="$('#edit').hide();"><i class="layui-icon">&#x1006;</i></span>
                    <div class="source-head">
                        <label class="layui-form-label"><strong>修改</strong></label>
                    </div>
                    <div class="source-body">
                        <div class="source-cont">
                            <div class="layui-form-item tab-head">
                                <div id="material_type" class="layui-input-block">
                                  <span class="filter-menu"><input type="radio" name="type" value="1" title="图文" checked=""></span>
                                  <span class="filter-menu"><input type="radio" name="type" value="2" title="视频"></span>
                                  <span class="filter-menu"><input type="radio" name="type" value="0" title="纯文字"></span>
                                </div>
                            </div>
                            <div class="tab-body">
                                <div class="tab-cont on">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">标题</label>
                                        <div class="layui-input-block">
                                          <input id="material_tit" type="text" name="title" lay-verify="title" autocomplete="off" placeholder="可不填" class="layui-input">
                                          <input id="material_id" type="hidden" name="id" lay-verify="title">
                                        </div>
                                    </div>
                                    <div class="layui-form-item layui-form-text">
                                        <label class="layui-form-label">文字</label>
                                        <div class="layui-input-block">
                                          <textarea id="material_con" placeholder="点击输入" name="content" class="layui-textarea"></textarea>
                                        </div>
                                    </div>
                                    <div class="layui-form-item" id="media">
                                        <label class="layui-form-label">图片</label>
                                        <span class="layui-btn ws-uploads-btn">
                                            <i class="layui-icon">&#xe64a;</i>
                                            上传图片
                                            <!-- <input type="file" value="" class="ws-uploads" title=" "> -->
                                            <input id="material_img" type="hidden" id="filename" lay-verify="required|number" name="filename" value="">
                                        </span>
                                        <div class="layui-input-block">
                                            <ul id="material_link" class="ws-images">
                                                <!-- 这里是图片 -->
                                            </ul>
                                        </div>
                                    <!-- </div> -->
                                    </div>
                                    <div class="layui-form-item layui-form-text">
                                        <label class="layui-form-label">评论内容</label>
                                        <div class="layui-input-block">
                                          <textarea id="material_com" placeholder="点击输入" lay-verify="required" name="comment" class="layui-textarea"></textarea>
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">素材小组</label>
                                        <div id="material_zhu" class="layui-input-inline">
                                          <select name="cate_id" lay-verify="required" lay-filter="aihao">
                                            <option value=""></option>
                                            <!-- <option value="0" selected="">默认分组</option> -->
                                            {volist name="list" id="data"}
                                            <option value="{$data.id}">{$data.cate_name}</option>
                                            {/volist}
                                            <!-- <option value="2">222</option> -->
                                          </select>
                                        </div>
                                        <!-- <button class="layui-btn layui-btn-primary">新建分组</button> -->
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label"></label>
                                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="*">导入</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

<script type="text/javascript" src="{:_static()}/layui/layui.js"></script>
<script type="text/babel" type="text/javascript" src="{:_static()}/js/common.js"></script>
<script type="text/babel" type="text/javascript">
    var form;
    layui.config({
        base : "js/"
    }).use(['form','layer','jquery','laypage','upload'],function(){
        form = layui.form(),
            layer = parent.layer === undefined ? layui.layer : parent.layer,
            laypage = layui.laypage;
            $ = layui.jquery;
            // var $ = layui.$;
            
            laydate = layui.laydate,
            taskName={},
            store={}, // 一个容器
            files=[]; // 文件上传表单
        // 文件上传
        util.uploads(["{:url('loads')}","{:url('delImage')}"],$,function(data){
            let id=data.id,link=data.link;
            files.push(id);
            $("#filename").val(id);
        }).then(id=>{
            files.forEach((item,i)=>{
                if(item==id){
                    delete files[i];
                }
            })
            console.log(files);
        })


        //添加设备
        $(".sourceAdd_btn").click(function(){
            layer.prompt({title: '新建小组'}, function(val, index){
                // layer.msg('得到了'+val);
                // 获取用户输入值
                // 新建loading
                //loading层
                var index2 = layer.load(1);
                // ajax提交添加
                $.ajax({
                    url:'{:url("member/material/addcate")}',
                    type:'GET',
                    data:{'cate_name':val},
                    dataType:'json',
                    success:function(data){
                        console.log(data)
                        // 关闭loading
                        layer.close(index2);
                        // 根据返回结果提示操作是否成功
                        if (data.code == 1) {
                            // 添加成功
                            // 关闭输入弹窗
                            layer.close(index);
                            layer.msg('添加素材小组成功');
                        }
                        else{
                            // 添加失败
                            layer.msg('添加素材小组失败');
                        }
                    }
                });
              
            });
        })

        //全选
        form.on('checkbox(allChoose)', function(data){
            var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
            child.each(function(index, item){
                item.checked = data.elem.checked;
            });
            form.render('checkbox');
        });

        //通过判断文章是否全部选中来确定全选按钮是否选中
        form.on("checkbox(choose)",function(data){
            var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
            var childChecked = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"]):checked')
            if(childChecked.length == child.length){
                $(data.elem).parents('table').find('thead input#allChoose').get(0).checked = true;
            }else{
                $(data.elem).parents('table').find('thead input#allChoose').get(0).checked = false;
            }
            form.render('checkbox');
        })

        //是否展示
        form.on('switch(isShow)', function(data){
            var index = layer.msg('修改中，请稍候',{icon: 16,time:false,shade:0.8});
            setTimeout(function(){
                layer.close(index);
                layer.msg("展示状态修改成功！");
            },2000);
        })
     
        //操作
        $("body").on("click",".news_edit",function(){  //编辑
            layer.alert('您点击了文章编辑按钮，由于是纯静态页面，所以暂时不存在编辑内容，后期会添加，敬请谅解。。。',{icon:6, title:'文章编辑'});
        })

        $("body").on("click",".news_collect",function(){  //收藏.
            if($(this).text().indexOf("已收藏") > 0){
                layer.msg("取消收藏成功！");
                $(this).html("<i class='layui-icon'>&#xe600;</i> 收藏");
            }else{
                layer.msg("收藏成功！");
                $(this).html("<i class='iconfont icon-star'></i> 已收藏");
            }
        })

        $("body").on("click",".news_del",function(){  //删除
            var _this = $(this);
            layer.confirm('确定删除此信息？',{icon:3, title:'提示信息'},function(index){
                //_this.parents("tr").remove();
                for(var i=0;i<newsData.length;i++){
                    if(newsData[i].newsId == _this.attr("data-id")){
                        newsData.splice(i,1);
                        newsList(newsData);
                    }
                }
                layer.close(index);
            });
        })


        // 批量删除
        $('body').on('click','.sourceDelete_btn',function(){
            layer.confirm('您确定要删除选中的素材吗？', {
                btn: ['确定', '取消']
                ,yes: function(index, layero){
                    // 获取选中的复选框
                    var id = '';
                    // var standardsName = '';
                    $("input:checkbox[name='delcheck']:checked").each(function() { // 遍历name=delcheck的多选框
                        id += $(this).val() + ',';
                        // console.log(this);
                        // standardsName += ',' + $(this).attr('title');
                    });
                    // 删除第一个字符
                    id = id.substring(0, id.length - 1); 
                    console.log(id)
                    //loading层
                    var index2 = layer.load(1);
                    // ajax提交添加
                    $.ajax({
                        url:'{:url("member/material/matedel")}',
                        type:'POST',
                        data:{'id':id},
                        dataType:'json',
                        success:function(data){
                            console.log(data)
                            // 关闭loading
                            layer.close(index2);
                            // 根据返回结果提示操作是否成功
                            if (data.code == 1) {
                                // 添加成功
                                // 关闭输入弹窗
                                // layer.close(index);
                                layer.msg(data.msg);

                                if (data.wait != '') {
                                    ttime = data.wait*1000;
                                }
                                else{
                                    ttime = 2000;
                                }
                                // 刷新当前页
                                window.setTimeout("window.location.reload()",ttime); 
                                // window.location.reload()
                            }
                            else{
                                // 添加失败
                                layer.msg(data.msg);
                            }
                        }
                    });
                }
            }, function(index){
                //按钮【取消】的回调
            });
        })


        // 修改
        form.on('submit(*)', function(data){
            // console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
            // console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            // return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
            
            // 新建loading
            //loading层
            var index2 = layer.load(1);
            // ajax提交添加
            $.ajax({
                url:'{:url("member/material/edit")}',
                type:'POST',
                data:data.field,
                dataType:'json',
                success:function(data){
                    console.log(data)
                    // 关闭loading
                    layer.close(index2);
                    // 根据返回结果提示操作是否成功
                    if (data.code == 1) {
                        // 修改成功
                        // 关闭输入弹窗
                        // layer.close(index);
                        layer.msg('修改成功');
                        window.location.href = data.url;
                    }
                    else{
                        // 修改失败
                        layer.msg('修改失败');
                    }
                }
            });
              
        });


        // 选择筛选条件
        form.on('select(cate)', function(data){
            var index2 = layer.load(1);
            // console.log(data.elem); //得到select原始DOM对象
            // console.log(data.value); //得到被选中的值
            // console.log(data.othis); //得到美化后的DOM对象
            // var urls = '{:url("member/material/manage")}';
            {if condition="$cateid neq ''"}
            // var urls = '{:url("member/material/manage")}';
            var urls = '{:url("member/material/manage",array('cates'=>$cateid))}';
            {else/}
            var urls = '{:url("member/material/manage")}';
            {/if}
            var cate = {'cate':data.value}

            // 获取用户选择的素材类型和素材小组
            var type = $("#sucai_type option:selected").val();
            var cate = data.value;
            // console.log(type+' +++ '+cate);
            var data;
            if (type != '' && cate != '') {
                data = {'type':type,'cate':cate};
            }
            else if (type != '' && cate == '') {
                data = {'type':type};
            }
            else if (type == '' && cate != '') {
                data = {'cate':cate};
            }
            else{
                return false;
            }
            
            $.ajax({
                url:urls,
                type:'GET',
                data:data,
                dataType:'json',
                success:function(list){
                    // console.log(list)
                    // console.log(curr)
                    mainHtml(list);
                    laypages(list.last_page);
                    layer.close(index2);
                    // $('#mainhtml').on('ifChanged', function(event) {
                    //     var $input = $('#mainhtml tr td').find('input');
                    //     $input.iCheck(event.currentTarget.checked ? 'check': 'uncheck');
                    // });
                }
            });
        });


        // 选择素材类型
        form.on('select(type)', function(data){
            var index2 = layer.load(1);
            // console.log(data.elem); //得到select原始DOM对象
            // console.log(data.value); //得到被选中的值
            // console.log(data.othis); //得到美化后的DOM对象
            // var urls = '{:url("member/material/manage")}';
            {if condition="$cateid neq ''"}
            // var urls = '{:url("member/material/manage")}';
            var urls = '{:url("member/material/manage",array('cates'=>$cateid))}';
            {else/}
            var urls = '{:url("member/material/manage")}';
            {/if}
            var type = {'type':data.value}


            // 获取用户选择的素材类型和素材小组
            var type = $("#sucai_type option:selected").val();
            var cate = $("#sucai_cate option:selected").val();
            // console.log(type+' +++ '+cate);
            var data;
            if (type != '' && cate != '') {
                data = {'type':type,'cate':cate};
            }
            else if (type != '' && cate == '') {
                data = {'type':type};
            }
            else if (type == '' && cate != '') {
                data = {'cate':cate};
            }
            else{
                return false;
            }
            
            $.ajax({
                url:urls,
                type:'GET',
                data:data,
                dataType:'json',
                success:function(list){
                    // console.log(list)
                    // console.log(curr)
                    mainHtml(list);
                    laypages(list.last_page);
                    layer.close(index2);
                    // $('#mainhtml').on('ifChanged', function(event) {
                    //     var $input = $('#mainhtml tr td').find('input');
                    //     $input.iCheck(event.currentTarget.checked ? 'check': 'uncheck');
                    // });
                }
            });
        });

        // function flushPage(page){
        //     var pageNum;
        //     $.get("getPageNum",function(data){
        //         pageNum=data;
        //     });
        //     $.get("getContent?page="+page,function(data){
        //         //渲染数据
        //     });
        //     layui.use(['laypage', 'layer'], function(){
        //         var laypage = layui.laypage
        //         ,layer = layui.layer;
        //         laypage({
        //             cont:'page',//容器。值可以传入元素id或原生DOM或jquery对象
        //             pages:pageNum,//把新的页数写入
        //             jump:function(obj) {//点击页数按钮触发的函数
        //                 var curr = obj.curr;//得到点击的页数
        //                 flushPage(curr);
        //             }
        //         });
        //     });
        // }

        // //分页
        // laypage({
        //     cont: 'pagination1'
        //     ,pages: {$page.last_page} //总页数
        //     ,curr : {$page.current_page}//当前页
        //     ,groups: 5 //连续显示分页数
        //     ,jump: function(obj, first){
        //         //得到了当前页，用于向服务端请求对应数据
        //         var curr = obj.curr;
        //         // console.log(first)//异步请求返回给后台
        //         if (!first) {
        //             {if condition="$cateid neq ''"}
        //             var urls = '{:url("member/material/manage",array('cateid'=>$cateid))}';
        //             {else/}
        //             var urls = '{:url("member/material/manage")}';
        //             {/if}
        //             $.ajax({
        //                 url:urls,
        //                 type:'GET',
        //                 data:{'page':curr},
        //                 dataType:'json',
        //                 success:function(data){
        //                     // console.log(data)
        //                     // console.log(curr)
        //                     mainHtml(data);
        //                     // $('#mainhtml').on('ifChanged', function(event) {
        //                     //     var $input = $('#mainhtml tr td').find('input');
        //                     //     $input.iCheck(event.currentTarget.checked ? 'check': 'uncheck');
        //                     // });
        //                 }
        //             });
        //         }
        //     }
        // }); 
        laypages({$page.last_page});
    })

    
    // 循环生成html
    function mainHtml(data){

        var htmls = '';
        // 循环获取数据
        $.each(data.data,function(j,item){
            // console.log(item);
            // console.log(j);
            htmls += '<tr>';
                htmls += '<td><input type="checkbox" value="'+item.id+'" name="delcheck" lay-skin="primary"></td>';
                htmls += '<td>'+item.id+'</td>';
                if (item.type == '0') {
                    htmls += '<td>文字</td>';
                }
                else if (item.type == '1'){
                    htmls += '<td>图片</td>';
                }
                else if (item.type == '2'){
                    htmls += '<td>视频</td>';
                }
                
                htmls += '<td>'+item.cate_name+'</td>';
                htmls += '<td>'+item.title+'</td>';
                htmls += '<td>'+item.content+'</td>';

                if (item.mtype == '0' || item.mtype == '' || item.mtype == null) {
                    htmls += '<td>无</td>';
                }
                else if (item.mtype == '1'){
                    htmls += '<td><img src="/uploads'+item.link+'" width="90"></td>';
                }
                else if (item.mtype == '2'){
                    htmls += '<td><video width="90" height="60" controls="controls" src="/uploads'+item.link+'"></video></td>';
                }
                htmls += '<td>'+item.comment+'</td>';
                htmls += '<td>';
                    htmls += '<button class="layui-btn layui-btn-normal layui-btn-mini" onclick="edit('+item.id+');"><i class="layui-icon"></i>编辑</button>';
                    htmls += '<button class="layui-btn layui-btn-danger layui-btn-mini" onclick="del('+item.id+');"><i class="layui-icon"></i>删除</button>';
                htmls += '</td>';
            htmls += '</tr>';

        })

        //这里获取到数据执行显示
        $("#mainhtml").html(htmls)
        form.render('checkbox');

        // layui.use(['laypage', 'layer'], function(){
        //     var laypage = layui.laypage
        //     ,layer = layui.layer;
        //     laypage({
        //         cont:'pagination1',//容器。值可以传入元素id或原生DOM或jquery对象
        //         pages:data.last_page,//把新的页数写入
        //     });
        // });
    }

    function laypages(last_page){
        layui.use(['laypage', 'layer'], function(){
            var laypage = layui.laypage
            ,layer = layui.layer;
            laypage({
                cont:'pagination1'//容器。值可以传入元素id或原生DOM或jquery对象
                ,pages:last_page//把新的页数写入
                ,groups: 5 //连续显示分页数
                ,jump: function(obj, first){
                    //得到了当前页，用于向服务端请求对应数据
                    var curr = obj.curr;
                    // console.log(first)//异步请求返回给后台
                    if (!first) {
                        var index2 = layer.load(1);
                        {if condition="$cateid neq ''"}
                        // var urls = '{:url("member/material/manage")}';
                        var urls = '{:url("member/material/manage",array('cates'=>$cateid))}';
                        {else/}
                        var urls = '{:url("member/material/manage")}';
                        {/if}

                        // 获取用户选择的素材类型和素材小组
                        var type = $("#sucai_type option:selected").val();
                        var cate = $("#sucai_cate option:selected").val();
                        console.log(type+' +++ '+cate);
                        var data;
                        if (type != '' && cate != '') {
                            data = {'page':curr,'type':type,'cate':cate};
                        }
                        else if (type != '' && cate == '') {
                            data = {'page':curr,'type':type};
                        }
                        else if (type == '' && cate != '') {
                            data = {'page':curr,'cate':cate};
                        }
                        else{
                            data = {'page':curr};
                        }
                        // return false;
                        // console.log(data);

                        $.ajax({
                            url:urls,
                            type:'GET',
                            data:data,
                            dataType:'json',
                            success:function(data){
                                // console.log(data)
                                // console.log(curr)
                                mainHtml(data);
                                layer.close(index2);
                                // $('#mainhtml').on('ifChanged', function(event) {
                                //     var $input = $('#mainhtml tr td').find('input');
                                //     $input.iCheck(event.currentTarget.checked ? 'check': 'uncheck');
                                // });
                            }
                        });
                    }
                }
            });
        });
    }


    // 编辑
    function edit(id){
        
        // ajax获取素材信息
        //loading层
        var index2 = layer.load(1);
        // ajax提交添加
        $.ajax({
            url:'{:url("member/material/findmaterial")}',
            type:'POST',
            data:{'id':id},
            dataType:'json',
            success:function(data){
                // console.log(data)
                // 关闭loading
                layer.close(index2);
                // 根据返回结果提示操作是否成功
                if (data.code == 0) {
                    // 添加失败
                    layer.msg(data.msg);
                }
                else{
                    // 查找成功
                    // 生成html数据
                    $('#material_id').val(data.source.id)
                    $('#material_tit').val(data.source.title)
                    $('#material_con').val(data.source.content)
                    $('#material_com').val(data.source.comment)
                    $('#material_img').val(data.source.mid)

                    var mediaHtml = '';
                    if(data.source.type == 0){
                        $("#media").html('');
                        $("#material_type").html('<span class="filter-menu"><input type="radio" name="type" value="1" title="图文"></span><span class="filter-menu"><input type="radio" name="type" value="2" title="视频"></span><span class="filter-menu"><input type="radio" name="type" value="0" title="纯文字" checked=""></span>');

                                  
                    }
                    else if(data.source.type == 1){
                        // 图片
                        mediaHtml += '<label class="layui-form-label">图片</label>';
                        mediaHtml += '<span class="layui-btn ws-uploads-btn">';
                            mediaHtml += '<i class="layui-icon">&#xe64a;</i>';
                            mediaHtml += '上传图片';
                            mediaHtml += '<input type="file" value="" class="ws-uploads" title=" ">';
                            mediaHtml += '<input type="hidden" id="filename" lay-verify="required|number" name="filename" value="'+data.source.mid+'">';
                        mediaHtml += '</span>';
                        mediaHtml += '<div class="layui-input-block">';
                            mediaHtml += '<ul id="material_link" class="ws-images">';
                                // <!-- 这里是图片 -->
                                mediaHtml += '<li><span class="close" data-id="'+data.source.mid+'">x</span><img src="/uploads'+data.source.link+'"></li>';
                            mediaHtml += '</ul>';
                        mediaHtml += '</div>';


                        $("#material_type").html('<span class="filter-menu"><input type="radio" name="type" value="1" title="图文" checked=""></span><span class="filter-menu"><input type="radio" name="type" value="2" title="视频"></span><span class="filter-menu"><input type="radio" name="type" value="0" title="纯文字"></span>');

                        // $('#material_link').html('<li><span class="close" data-id="'+data.source.mid+'">x</span><img src="/uploads'+data.source.link+'"></li>')
                    }
                    else if(data.source.type == 2){
                        // 视频
                        mediaHtml += '<label class="layui-form-label">视频</label>';
                        mediaHtml += '<span class="layui-btn ws-uploads-btn">';
                            mediaHtml += '<i class="layui-icon">&#xe652;</i>';
                            mediaHtml += '上传视频';
                            mediaHtml += '<input type="file" value="" class="ws-uploads" title=" ">';
                            mediaHtml += '<input type="hidden" id="filename" lay-verify="required|number" name="filename" value="'+data.source.mid+'">';
                        mediaHtml += '</span>';
                        mediaHtml += '<div class="layui-input-block">';
                            mediaHtml += '<ul id="material_link" class="ws-images">';
                                // <!-- 这里是视频 -->
                                mediaHtml += '<li><span class="close" data-id="286">x</span><img src="http://www.yk.com/uploads/20180604/8af963ecd5c1bc361a7b56486ae254bb.png"></li>';
                            mediaHtml += '</ul>';
                        mediaHtml += '</div>';

                        $("#material_type").html('<span class="filter-menu"><input type="radio" name="type" value="1" title="图文"></span><span class="filter-menu"><input type="radio" name="type" value="2" title="视频" checked=""></span><span class="filter-menu"><input type="radio" name="type" value="0" title="纯文字"></span>');

                        // $('#material_link').html('')
                    }
                    $("#media").html(mediaHtml);

                    var zhuHtml = '';
                    zhuHtml += '<select name="cate_id" lay-verify="required" lay-filter="aihao">';
                    $.each(data.cate,function(j,item){
                        if (data.source.cate_id == item.id) {
                            zhuHtml += '<option value="'+item.id+'" selected="selected">'+item.cate_name+'</option>';
                        }
                        else{
                            zhuHtml += '<option value="'+item.id+'">'+item.cate_name+'</option>';
                        }
                    })
                    zhuHtml += '</select>';
                    $("#material_zhu").html(zhuHtml);

                                      // <select name="cate_id" lay-verify="required" lay-filter="aihao">
                                      //   <option value=""></option>
                                      //   <!-- <option value="0" selected="">默认分组</option> -->
                                      //   {volist name="list" id="data"}
                                      //   <option value="{$data.id}">{$data.cate_name}</option>
                                      //   {/volist}
                                      //   <!-- <option value="2">222</option> -->
                                      // </select>

                    // 显示编辑框
                    $('#edit').show();
                    form.render();
                    // window.location.reload()
                }
            }
        });
        

    }

    // 删除
    function del(id){
        layer.confirm('您确定要删除这个素材吗？', {
            btn: ['确定', '取消']
            ,yes: function(index, layero){
                
                //loading层
                var index2 = layer.load(1);
                // ajax提交添加
                $.ajax({
                    url:'{:url("member/material/matedel")}',
                    type:'POST',
                    data:{'id':id},
                    dataType:'json',
                    success:function(data){
                        console.log(data)
                        // 关闭loading
                        layer.close(index2);
                        // 根据返回结果提示操作是否成功
                        if (data.code == 1) {
                            // 添加成功
                            // 关闭输入弹窗
                            // layer.close(index);
                            layer.msg(data.msg);

                            if (data.wait != '') {
                                ttime = data.wait*1000;
                            }
                            else{
                                ttime = 2000;
                            }
                            // 刷新当前页
                            window.setTimeout("window.location.reload()",ttime); 
                            // window.location.reload()
                        }
                        else{
                            // 添加失败
                            layer.msg(data.msg);
                        }
                    }
                });
            }
        }, function(index){
            //按钮【取消】的回调
        });
        // alert(id);
    }



    // 用户选择素材类型后的更改
    $(".tab-head .filter-menu").click(function(){
        var index = $(this).index();
        // $(".tab-body .tab-cont").eq(index).addClass("on")
            // .siblings().removeClass("on");
            // console.log(index)

        $("#media").html('');
        var mediaHtml = '';
        if (index == 0) {
            // 图片
            mediaHtml += '<label class="layui-form-label">图片</label>';
            mediaHtml += '<span class="layui-btn ws-uploads-btn">';
                mediaHtml += '<i class="layui-icon">&#xe64a;</i>';
                mediaHtml += '上传图片';
                mediaHtml += '<input type="file" value="" class="ws-uploads" title=" ">';
                mediaHtml += '<input type="hidden" id="filename" lay-verify="required|number" name="filename" value="">';
            mediaHtml += '</span>';
            mediaHtml += '<div class="layui-input-block">';
                mediaHtml += '<ul id="material_link" class="ws-images">';
                    // <!-- 这里是图片 -->
                mediaHtml += '</ul>';
            mediaHtml += '</div>';
        }
        else if (index == 1){
            // 视频
            mediaHtml += '<label class="layui-form-label">视频</label>';
            mediaHtml += '<span class="layui-btn ws-uploads-btn">';
                mediaHtml += '<i class="layui-icon">&#xe652;</i>';
                mediaHtml += '上传视频';
                mediaHtml += '<input type="file" value="" class="ws-uploads" title=" ">';
                mediaHtml += '<input type="hidden" id="filename" lay-verify="required|number" name="filename" value="">';
            mediaHtml += '</span>';
            mediaHtml += '<div class="layui-input-block">';
                mediaHtml += '<ul id="material_link" class="ws-images">';
                    // <!-- 这里是图片 -->
                mediaHtml += '</ul>';
            mediaHtml += '</div>';

            // <!-- 上传视频 -->
            // <video width="320" height="240" controls="controls" src="">  
            // </video>
        }
        $("#media").html(mediaHtml);
    });

</script>
{/block}